package com.yf.config.mybatisplus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chentianwei
 * @date 2024-08-14 15:49
 * @description 用于MyBatisPlus的配置文件
 */
@Configuration
@Slf4j
public class MBPConfig {

    /**
     * MyBatisPlus拦截器（用于分页）
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加MySQL的分页拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    @Primary
    public MybatisPlusProperties mybatisPlusProperties(){

        MybatisPlusProperties mybatisPlusProperties = new MybatisPlusProperties();

        List<String> locations = new ArrayList<>();
        locations.add("classpath*:mapper/*.xml");
        String[] property = locations.toArray(String[]::new);

        mybatisPlusProperties.setMapperLocations(property);

        log.info("MyBatisPlus Mapper路径: --> " + Arrays.toString(property));

        return mybatisPlusProperties;
    }

}
